package com.ving.test.mybatis01.binding;

import com.ving.test.mybatis.session.DefaultqlSeesion;
import com.ving.test.mybatis01.session.DefaultSqlSeesion01;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * Created by liuweijie on 2019-11-19.
 */
public class MapperProxy01<T> implements InvocationHandler {

    private final DefaultSqlSeesion01 sqlSession;
    private final Class<T> mapperInterface;

    public MapperProxy01(DefaultSqlSeesion01 sqlSession, Class<T> mapperInterface) {
        this.sqlSession = sqlSession;
        this.mapperInterface = mapperInterface;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("1111");
        String key = method.getDeclaringClass().getName()+"."+method.getName();
        System.out.println("namespace+id: "+key);
        MapperMethod01 mapperMethod01 = sqlSession.getConfiguration01().getMybatisRegister01().getMapperRegister().get(key);
        if(mapperMethod01!=null){
            return sqlSession.selectOne(mapperMethod01,String.valueOf(args[0]));

        }

        System.out.println("222");

        return method.invoke(proxy,args);
    }




}
